package day7;

import day1.Dog;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * @author oldliu
 * @since 1.0
 */
public class DogTest3 {
    public static void main(String[] args) throws Exception {
        Class<Dog> dogClass = Dog.class;
        //Dog dog = dogClass.newInstance();//new Dog
        //dog.show();
        //找到那个四个参数的构造方法
//        Constructor<Dog> ctr = dogClass.getDeclaredConstructor(String.class, int.class, int.class, String.class);
//        Dog dog=ctr.newInstance("大黄",100,99,"土狗");
//        dog.show();
        Constructor<Dog> ctr = dogClass.getDeclaredConstructor(String.class,String.class);
        ctr.setAccessible(true);//突破访问限制，对系统类无效
        Dog dog=ctr.newInstance("大黄","土狗");
        dog.show();
        System.out.println(">>>>>>>>>>>>>");
        Field f = dogClass.getSuperclass().getDeclaredField("health");
        f.setAccessible(true);
        f.setInt(dog,64);
        //f.set
        dog.show();
        System.out.println(">>>>>>>>>>>>>");

        Method method=dogClass.getDeclaredMethod("setHealthAndLove",int.class,int.class);
        method.setAccessible(true);
        Object rtn=method.invoke(dog,88,80);
        System.out.println("返回了"+rtn);
        dog.show();
    }
}
